package qg.fangrui.springboot.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import qg.fangrui.springboot.model.User;
import qg.fangrui.springboot.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created by zggdczfr on 2017/2/28.
 */
@RestController
public class RedisController {

    @Autowired
    private UserService userService;

    @Value("${server.port}")
    String port;

    
    //同时启动两个相同的工程(比如：8080端口与9090端口)，访问 http://localhost:8080/session 与 http://localhost:9090/session 
    //我们可以得到以下结果：结果中的SessionId是一致的，却是由两个不同项目工程来提供服务。这样子，SpringSession 利用拦截器 Filter
    //帮我们在每个请求前进行了同步设置，达到了分布式系统中 session 共享,session共享实现之后便可以做单点登录了
    
    
    //用户第一次登陆后把用户信息存入session中,session会被存入redis缓存中，下次再有用户登陆时，就会根据sessionid去redis缓存中查找信息
    //单点登录：不同系统使用同一个redis服务，使用@EnableRedisHttpSession注解后，只要是同一客户端，登陆使用同一redis服务做缓存的系统，sessionid都是
    //一样的，这样就保证了不同系统session共享
    @RequestMapping(value = "/first", method = RequestMethod.GET)
    public Object getSession(HttpServletRequest request){
        Map<String, Object> map = new HashMap<String, Object>();
        request.getSession().setAttribute("userName", "张三");
        request.getSession().setAttribute("userAge", 18);
        map.put("SessionId", request.getSession().getId());
        map.put("ServerPort", "服务端口号为 "+port);
        return map;
    }

    @RequestMapping(value = "/session", method = RequestMethod.GET)
    public Object getSessionSecond(HttpServletRequest request){
        Map<String, Object> map = new HashMap<String, Object>();
       System.out.println(request.getSession().getAttribute("userName")+"====================");
       System.out.println(request.getSession().getAttribute("userAge")+"======================"); 
        map.put("SessionId", request.getSession().getId());
        map.put("ServerPort", "服务端口号为 "+port);
        return map;
    }

    
    
    
    @RequestMapping(value = "/redis/{uuid}", method = RequestMethod.GET)
    public String redisTest(@PathVariable("uuid") String uuid){
        System.out.println("====== 进行 Redis 缓存试验 ======");
     
        //查询
        User user = userService.findByUuid(uuid);
        System.out.println(user.toString());
        
        return "success";
    }
    
    @RequestMapping(value = "/update/{name}", method = RequestMethod.GET)
    @ResponseBody
    public String update(@PathVariable("name") String name){
    	
    	User user = new User();
    	user.setUuid("182454d918d049a6a10919a9274c6df3");
    	user.setName(name);
    	user.setAge(15);
    	userService.update(user);
    	
    	return user.toString();
    }
    //缓存集合
    @RequestMapping(value = "/redis/list", method = RequestMethod.GET)
    public String redisList(){
        System.out.println("====== 进行 Redis 缓存集合试验 ======");
     
        //查询
        List<User> userlist = userService.findByAge(15);
        System.out.println(userlist.toString());
        
        return userlist.toString();
    }
    
    @RequestMapping(value = "/save", method = RequestMethod.GET)
    public String save(){
    	 User user = new User();
         //生成第一个用户的唯一标识符 UUID
         String u1_uuid = UUID.randomUUID().toString();
         //去掉 UUID 的 - 符号
         String uuid1 = u1_uuid.substring(0,8)+u1_uuid.substring(9,13)+u1_uuid.substring(14,18)+u1_uuid.substring(19,23)+u1_uuid.substring(24);
         user.setUuid(uuid1);
         user.setAge(24);
         user.setName("朱建峰");

         
         try {
             userService.save(user);
         } catch (Exception e) {
             System.out.println("保存用户出现异常");
         }
         
         return user.toString();
         
    }
    
    
    
}
